package com.eningqu.aipen.logic.network;

import android.util.Log;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Package:com.eningqu.aipen.logic.network
 * Author:starr
 * Time:2023/10/4  11:54
 * Description: 多个Retrofit共享的okhttp
 */
public class ShareOkHttpClient {
    private static volatile OkHttpClient mInstance = null;

    public static OkHttpClient getClient(){
        if(mInstance==null){
            synchronized (ShareOkHttpClient.class){
                if(mInstance==null){
                    OkHttpClient.Builder builder = new OkHttpClient.Builder()
                            .connectTimeout(30, TimeUnit.SECONDS)//连接超时时间6s
                            .readTimeout(30, TimeUnit.SECONDS)
                            .writeTimeout(30, TimeUnit.SECONDS);
                    //根据需要可以设置其他的拦截器
                    //...
                    //日志拦截器
                    HttpLoggingInterceptor.Level level = HttpLoggingInterceptor.Level.BODY;
                    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(message ->
                            Log.d("RetrofitMessage", "Okhttp====Message:" + message));
                    loggingInterceptor.setLevel(level);
                    //定制builder
                    builder.addInterceptor(loggingInterceptor);
                    mInstance =  builder.build();
                }
            }
        }
        return mInstance;
    }
}
